﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TimeTableManagement.Entities;
using TimeTableManagement;
using System.Data.OleDb;

namespace TimeTableManagement.Controllers
{ 
    
    public class CourseController : Controller
    {
        private QLSVContext db = new QLSVContext();

        //
        // GET: /Course/

        public ViewResult Index()
        {
            var tb_course = db.Tb_Course.Include(t => t.Tb_Department).Include(k=>k.Tb_Config);
            return View(tb_course.ToList());
        }

        //
        // GET: /Course/Details/5

        public ViewResult Details(int id)
        {
            Tb_Course tb_course = db.Tb_Course.Find(id);
            return View(tb_course);
        }

        //
        // GET: /Course/Create

        public ActionResult Create()
        {
            
            ViewBag.id_department = new SelectList(db.Tb_Department, "id", "name");
            ViewBag.type = new SelectList(db.Tb_Config.Where(g => g.option.Equals("coursetype")), "id", "displayword");
            ViewBag.value = new SelectList(db.Tb_Config.Where(g => g.option.Equals("roomtype")), "id", "displayword");
            
            return View();
        } 

        //
        // POST: /Course/Create

        [HttpPost]
        public ActionResult Create(Tb_Course tb_course)
        {
            if (ModelState.IsValid)
            {
                db.Tb_Course.Add(tb_course);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.id_department = new SelectList(db.Tb_Department, "id", "name", tb_course.id_department);
            ViewBag.type = new SelectList(db.Tb_Config.Where(g => g.option.Equals("coursetype")), "id", "displayword", tb_course.type);
            return View(tb_course);
        }
        
        //
        // GET: /Course/Edit/5
 
        public ActionResult Edit(int id)
        {
            Tb_Course tb_course = db.Tb_Course.Find(id);
            ViewBag.id_department = new SelectList(db.Tb_Department, "id", "name", tb_course.id_department);
            ViewBag.type = new SelectList(db.Tb_Config.Where(g => g.option.Equals("coursetype")), "id", "displayword",tb_course.type);
            return View(tb_course);
        }

        //
        // POST: /Course/Edit/5

        [HttpPost]
        public ActionResult Edit(Tb_Course tb_course)
        {
            if (ModelState.IsValid)
            {
                db.Entry(tb_course).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.id_department = new SelectList(db.Tb_Department, "id", "name", tb_course.id_department);
            ViewBag.type = new SelectList(db.Tb_Config.Where(g => g.option.Equals("coursetype")), "id", "displayword", tb_course.type);
            return View(tb_course);
        }

        //
        // GET: /Course/Delete/5
 
        public ActionResult Delete(int id)
        {
            Tb_Course tb_course = db.Tb_Course.Find(id);
            return View(tb_course);
        }

        //
        // POST: /Course/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Tb_Course tb_course = db.Tb_Course.Find(id);
            db.Tb_Course.Remove(tb_course);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        //
        // GET: /Education/Import
        public ActionResult Import(string filename)
        {
            if (filename == null)
            {
                ViewData["kq"] = "no";
                return View();
            }
            else
            {
                int i = this.insertCourse(filename);
                if (i == 0) ViewData["kq"] = "ok";
                else ViewData["kq"] = "un";
                return View();
            }
        }

        private List<Tb_Course> getTb_Course(String fi)
        {
            List<Tb_Course> list = new List<Tb_Course>();

            //string connString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source="
            //    + Server.MapPath("~/Uploads/" + fi) + ";" + "Extended Properties=Excel 8.0;";
            string connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath("~/Uploads/" + fi) + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES\";";
            // Create the connection object
            OleDbConnection oledbConn = new OleDbConnection(connString);
            try
            {
                // Open connection
                oledbConn.Open();

                // Create OleDbCommand object and select data from worksheet Sheet1
                OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Sheet1$]", oledbConn);

                // Create new OleDbDataAdapter
                OleDbDataAdapter oleda = new OleDbDataAdapter();

                oleda.SelectCommand = cmd;

                // Create a DataSet which will hold the data extracted from the worksheet.
                DataSet ds = new DataSet();

                // Fill the DataSet from the data extracted from the worksheet.
                oleda.Fill(ds);

                int i = 0;
                DataRow r = ds.Tables[0].Rows[0];
                while (r != null)
                {
                    Array t = r.ItemArray;
                    Tb_Course _course = new Tb_Course();
                    _course.code = (string)t.GetValue(0);
                    _course.name = (string)t.GetValue(1);
                    _course.credit = Int32.Parse((string)t.GetValue(2));
                    string codedepar = (string)t.GetValue(3);
                    var _d = db.Tb_Department.Where(g=>g.code.Equals(codedepar));
                    foreach(Tb_Department item_d in _d)
                        if (item_d.code.Equals(codedepar))
                        {
                            _course.id_department = item_d.id;
                            break;
                        }
                    list.Add(_course);
                    i++;
                    r = ds.Tables[0].Rows[i];
                }
            }
            catch
            {
                //Response.Write(ex);
            }
            finally
            {
                oledbConn.Close();
            }

            return list;
        }
        private int insertCourse(String f)
        {
            QLSVContext _context = new QLSVContext();
            List<Tb_Course> list = new List<Tb_Course>();
            list = this.getTb_Course(f);
            int len = list.Count;
            int i = -1;
            foreach (Tb_Course u in list)
            {
                if (i == -1) i = 0;
                _context.Tb_Course.Add(u);
                _context.SaveChanges();
                i++;
            }
            _context.Dispose();
            return (i - len);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}